package com.zhengshang.api.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.zhengshang.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhengshang.api.mapper.AppBrowseRecordMapper;
import com.zhengshang.api.domain.AppBrowseRecord;
import com.zhengshang.api.service.IAppBrowseRecordService;

/**
 * 浏览记录Service业务层处理
 *
 * @author ruoyi
 * @date 2021-03-29
 */
@Service
public class AppBrowseRecordServiceImpl implements IAppBrowseRecordService
{
    @Autowired
    private AppBrowseRecordMapper appBrowseRecordMapper;

    /**
     * 查询浏览记录
     *
     * @param recordId 浏览记录ID
     * @return 浏览记录
     */
    @Override
    public AppBrowseRecord selectAppBrowseRecordById(Long recordId)
    {
        return appBrowseRecordMapper.selectAppBrowseRecordById(recordId);
    }

    /**
     * 查询浏览记录列表
     *
     * @param appBrowseRecord 浏览记录
     * @return 浏览记录
     */
    @Override
    public List<AppBrowseRecord> selectAppBrowseRecordList(AppBrowseRecord appBrowseRecord)
    {
        List<AppBrowseRecord> appBrowseRecordList = appBrowseRecordMapper.selectAppBrowseRecordList(appBrowseRecord);
        return appBrowseRecordList == null ? new ArrayList<>() : appBrowseRecordList;
    }

    /**
     * 新增浏览记录
     *
     * @param appBrowseRecord 浏览记录
     * @return 结果
     */
    @Override
    public int insertAppBrowseRecord(AppBrowseRecord appBrowseRecord)
    {
        //先删除相同物品的浏览记录
        appBrowseRecordMapper.deleteByUserIdAndInfoId(appBrowseRecord);
        appBrowseRecord.setCreateTime(DateUtils.getNowDate());
        return appBrowseRecordMapper.insertAppBrowseRecord(appBrowseRecord);
    }

    /**
     * 修改浏览记录
     *
     * @param appBrowseRecord 浏览记录
     * @return 结果
     */
    @Override
    public int updateAppBrowseRecord(AppBrowseRecord appBrowseRecord)
    {
        return appBrowseRecordMapper.updateAppBrowseRecord(appBrowseRecord);
    }

    /**
     * 批量删除浏览记录
     *
     * @param recordIds 需要删除的浏览记录ID
     * @return 结果
     */
    @Override
    public int deleteAppBrowseRecordByIds(String[] recordIds)
    {
        if (recordIds.length == 0) return 0;
        Long[] idArr = new Long[recordIds.length];
        for (int i = 0; i < recordIds.length; i++) {
            try {
                idArr[i] = Long.parseLong(recordIds[i].trim());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return appBrowseRecordMapper.deleteAppBrowseRecordByIds(idArr);
    }

    /**
     * 批量删除浏览记录
     *
     * @param recordIds 需要删除的浏览记录ID
     * @return 结果
     */
    @Override
    public int deleteAppBrowseRecordByIds(Long[] recordIds)
    {
        return appBrowseRecordMapper.deleteAppBrowseRecordByIds(recordIds);
    }

    /**
     * 删除浏览记录信息
     *
     * @param recordId 浏览记录ID
     * @return 结果
     */
    @Override
    public int deleteAppBrowseRecordById(Long recordId)
    {
        return appBrowseRecordMapper.deleteAppBrowseRecordById(recordId);
    }
}